#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// 通达信日K线数据结构
typedef struct 
{
    int date;    // 年月日，如：20191228
    int open;    // 开盘价的100倍
    int high;    // 最高价的100倍
    int low;     // 最低价的100倍
    int close;   // 收盘价的100倍
    float vol1;  // 成交额，元
    int vol2;    // 成交量，股
    int reserve; // 保留字
}dayrecordstruct;

int main(){

    FILE *finput, *foutput;
    dayrecordstruct dayrecord;

    if ((finput=fopen("..//..//data//sh512880.day","rb"))==NULL)
    {
        printf("cant't open the input file");
        exit(0);
    }

    if ((foutput=fopen("..//..//data//output//sh512880.txt","w"))==NULL)
    {
        printf("cant't open the out file");
        exit(0);
    }

    fprintf(foutput,"trade_date,open,high,low,close,vol1,vol2,reserve\n");

    // int a;
    // a = sizeof(dayrecordstruct);
    // printf("%s",a);
    // printf("file opened!");

    while(fread(&dayrecord,sizeof(dayrecordstruct),1,finput)==1)
    {
        printf("%d  %.3f  %.3f  %.3f  %.3f  %.3f  %d  %d\n",dayrecord.date, 
                                    (float)dayrecord.open/1000,
                                    (float)dayrecord.high/1000,
                                    (float)dayrecord.low/1000,
                                    (float)dayrecord.close/1000,
                                    dayrecord.vol1,
                                    dayrecord.vol2,
                                    dayrecord.reserve);
        fprintf(foutput,"%d,%.3f,%.3f,%.3f,%.3f,%.3f,%d,%d\n",dayrecord.date, 
                                    (float)dayrecord.open/1000,
                                    (float)dayrecord.high/1000,
                                    (float)dayrecord.low/1000,
                                    (float)dayrecord.close/1000,
                                    dayrecord.vol1,
                                    dayrecord.vol2,
                                    dayrecord.reserve);
    }

    fclose(finput);
    fclose(foutput);

    return 0;

}